$(function(){
	branch(null);
})

function branch(data){
	var url="../../sumchase/selectAllBranch.action";
	$.post(url,data,function(mes){
		$.each(mes,function(i,item){
			$("#braId").append("<option value="+item.braId+">"+item.braName+"</option>");
			load();
		})
	},"json");
}

// 写layer的代码
layui.use(['form', 'layedit', 'laydate','table'], function(){
	 var table = layui.table
	  ,laydate = layui.laydate
	  ,form = layui.form
	  ,layer = layui.layer
	  ,layedit = layui.layedit;
	 
	  //日期
	  laydate.render({
	    elem: '#date'
	  });
	  
	  laydate.render({
		    elem: '#time'
	  });
	 
	  //退货表条件查询
	  var $ = layui.$, active = {
	    reload: function(){
	      var date = $('#date');
	      var time = $('#time');
	      var state= $('#braId');
	      //执行重载
	      table.reload('idTest', {
	        page: {
	          curr: 1 //重新从第 1 页开始
	        }
	        ,where: {
	        	//把值赋给模型类
	        	optime: date.val(),
	        	opdate: time.val(),
	        	braId: state.val()
	        }
	      });
	    }
	  };
	  
	  //退货表的查询
	  $('.demoTable .layui-btn').on('click', function(){
		    var type = $(this).data('type');
		    active[type] ? active[type].call(this) : '';
	  });
	  
	  
	  
	//监听工具条
	table.on('tool(demo)', function(obj){
		if(obj.event === "thxq"){
			layer.open({
    	        type: 1	 //此处以iframe举例
    	        ,title: '商品详情'
    	        ,offset: 'auto'
    	        ,area: ['1000px', '490px']
    	        ,content:$('#add2')
    	        ,success: function(layero){
    	        	//重载表单
    	        	 table.reload('idTest2', {
    	     	        page: {
    	     	          curr: 1 //重新从第 1 页开始
    	     	        },where: {
    	     	        	allId: obj.data.allId,
						}
    	     	      });
    	        },end : function() {
    	        	$("#add2").hide();
    	        }
    	    });
		}else if(obj.event === "shenhe"){
			var index = layer.open({
    	        type: 1	 //此处以iframe举例
    	        ,title: '财务审核'
    	        ,offset: 'auto'
    	        ,area: ['450px', '300px']
    	        ,content:$('#add3')
    	        ,success: function(layero){
    	        	$("#zuihoutijiao").click(function(){
    	        		var index2 = layer.load(0, {
    						  shade: [0.5,'#000'] //0.1是透明度      白色背景
      					});
		        		if($("#shenheyijian").val()!=''){
		        			var url="../../financesales/salesAddOrUpdate.action";
		        			var data={"csId":obj.data.csId,"allId":obj.data.allId,"auState":$("input[name='sex']:checked").val(),"csComment":$("#shenheyijian").val()};
		        			$.post(url,data,function(mes){
		        				if(mes==0){
		        					//重载表单
			           	        	  table.reload('idTest', {
			           	     	        page: {
			           	     	          curr: 1 //重新从第 1 页开始
			           	     	        }
			           	     	      });
			           	        	//关闭弹出层
			  	                    layer.close(layer.index);
			  	                    //刷新父页面
			  	                    location.reload();
			  	                  layer.msg('审核成功', {icon: 1});
		        				}else if(mes==1){
		        					layer.msg('失败', {icon: 2});
		        				}else if(mes==2){
		        					layer.msg('财务资金不足', {icon: 2});
		        				}
		        			},"json");
		        		}else{
    	        			$("#shenheyijian").val("");
    	        			layer.msg('请填写审核意见', {icon: 2});
    	        		}
		        		layer.close(index2);//关闭当前页
		        	});
    	        },end : function() {
	  	        	$("#add3").hide();
	  	        	$("#shenheyijian").val("");
	  	        }
	  	    });
		}
	});
	 
	 
})


//表单渲染 
function load() {
	 var form = layui.form
	 form.render();
}


